package test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.io.Files;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.util.StringUtils;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

/**
 * Created by urfresh.mark on 2017/2/27.
 */
public class MongoSave4 {
    
    public MongoClient mongClient() throws Exception {
        MongoClient client = new MongoClient(
                Arrays.asList(
                        new ServerAddress("120.26.8.17", 27017)),
                Arrays.asList(
                        MongoCredential.createCredential("app_event_track", "app_event_track", "601ae08bc3ee0f2".toCharArray())
                ),
                MongoClientOptions.builder()
                        .socketKeepAlive(true)
                        .connectionsPerHost(10)
                        .threadsAllowedToBlockForConnectionMultiplier(10)
                        .connectTimeout(50000)
                        .maxWaitTime(50000)
//                        .requiredReplicaSetName(mongoReplicaSet)
//                        .readPreference(ReadPreference.secondaryPreferred())
                        .socketTimeout(10000)
                        .build());
        return client;
    }
    
    public SimpleMongoDbFactory mongoFactory() throws Exception {
        return new SimpleMongoDbFactory(mongClient(), "app_event_track");
    }
    
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoFactory());
    }
    
    public static void main(String[] args) throws Exception {


        List<String> list = Files.readLines(new File("C:\\Users\\urfresh.mark\\Desktop\\sys_track.log"), Charset.forName("utf-8"));
        int total = list.size();
        int androidUser = 0;
        int iosUser = 0;
        int appIdUser = 0;
        List<String> newList = new ArrayList<>();
        List<String> list2 = new ArrayList<>(list.size());
        Set<String> set = new HashSet<>();

        int i=0;

        for(String s: list){
            set.add("{"+s.substring(43));
            System.out.println(i++);
        }

//        for(String s : list){
//            JSONObject o = JSON.parseObject(s);
//            String userName = o.getString("userName");
//            if(newList.contains(userName))
//                continue;
//            else {
//                System.out.println(i++);
//                list2.add(s);
//                newList.add(userName);
//            }
//        }
        for(String s : set){
            JSONObject o = JSON.parseObject(s);
            String androidId = o.getString("androidId");
            String idfa = o.getString("idfa");
            if(StringUtils.isEmpty(androidId) && StringUtils.isEmpty(idfa)){
                appIdUser ++;
            }else{
                if(StringUtils.isEmpty(androidId)==false)
                    androidUser++;
                else
                    iosUser++;
            }
        }
        System.out.println("总共用户："+set.size()+",android用户："+androidUser+"，ios用户"+iosUser+"，appId用户"+appIdUser);

    }
    
}
